package org.csu.mybigpro.controller;

import org.csu.mybigpro.DTO.ResourceAnalysisDto;
import org.csu.mybigpro.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
// 【修复】我们移除类级别的@RequestMapping，将完整路径直接放在方法上，以提高映射的明确性
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/api/resources/analyze")
    public ResponseEntity<?> analyzeResource(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("error", "上传的文件不能为空。"));
        }

        try {
            ResourceAnalysisDto result = resourceService.analyzeDocument(file);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body(Map.of("error", "文件分析失败: " + e.getMessage()));
        }
    }
}